<template>
  <div class="admig_Dialog">
    <!-- Attributes -->
    <!-- before-close	关闭前的回调，会暂停 Dialog 的关闭	function(done)，done 用于关闭 Dialog	—	— -->
    <!-- visible	是否显示 Dialog，支持 .sync 修饰符	boolean	—	false -->
    <el-dialog
      :title="isAdd ? '添加管理员' : '修改管理员'"
      :visible.sync="isShow"
      center
      :before-close="cancel"
    >
      <el-form :model="form" :rules="rules" ref="formData">
        <el-form-item
          label="所属角色"
          prop="roleid"
          :label-width="formLabelWidth"
        >
          <el-select v-model="form.roleid" placeholder="请选择">
            <el-option
              v-for="item in rolelist"
              :key="item.id"
              :label="item.rolename"
              :value="item.id"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item
          label="用户名称"
          prop="username"
          :label-width="formLabelWidth"
        >
          <el-input v-model="form.username" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="密码" :label-width="formLabelWidth">
          <el-input v-model="form.password" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="状态" :label-width="formLabelWidth">
          <el-switch
            v-model="form.status"
            active-color="#13ce66"
            inactive-color="gray"
            :active-value="1"
            :inactive-value="2"
          >
          </el-switch>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="cancel">取 消</el-button>
        <el-button type="primary" @click="confirm">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>
<script>
import { mapActions, mapGetters } from "vuex";
import { useradd, userinfo, useredit } from "../../request/api";
export default {
  data() {
    return {
      rules: {
        roleid: [
          { required: true, message: "请选择活动区域", trigger: "change" },
        ],
        username: [
          { required: true, message: "请输入用户名", trigger: "blur" },
          { min: 2, max: 8, message: "长度在 2 到 8 个字符", trigger: "blur" },
        ],
      },
      //   roleid角色编号number
      // username管理员名称string
      // password密码string
      // status状态  1正常2禁用number
      form: {
        roleid: "",
        username: "",
        password: "",
        status: 2,
      },
      formLabelWidth: "120px",
    };
  },
  computed: {
    ...mapGetters({
      rolelist: "role/getRole",
    }),
  },
  props: ["isShow", "isAdd"],
  mounted() {
    //触发角色行动
    if (this.rolelist && this.rolelist.length == 0) {
      this.getRoleAction();
    }
  },
  methods: {
    cancel() {
      this.$emit("cancel", false);
      this.reset();
    },
    reset() {
      this.form = {
        roleid: "",
        username: "",
        password: "",
        status: 2,
      };
      //   resetFields	对整个表单进行重置，将所有字段值重置为初始值并移除校验结果
      this.$refs.formData.resetFields();
    },
    ...mapActions({
      getAdmigAction: "admin/getAdmigAction",
      getRoleAction: "role/getRoleAction",
      getCountAction: "admin/getCountAction",
    }),
    confirm() {
      this.$refs.formData.validate((val) => {
        if (val) {
          if (this.isAdd) {
            //添加接口
            useradd(this.form).then((res) => {
              if (res.code == 200) {
                //添加成功
                this.$message.success(res.msg);
                //重新获取列表
                this.getAdmigAction();
                //重新获取总条数
                this.getCountAction();
                //关闭弹框、
                this.cancel();
              } else {
                this.$message.error(res.msg);
              }
            });
          } else {
            //编辑接口
            useredit(this.form).then((res) => {
              if (res.code == 200) {
                //添加成功
                this.$message.success(res.msg);
                //重新获取列表
                this.getAdmigAction();
                //重新获取总条数
                this.getCountAction();
                //关闭弹框
                this.cancel();
              } else {
                this.$message.error(res.msg);
              }
            });
          }
        }
      });
    },
    lookup(uid) {
      userinfo({ uid }).then((res) => {
        if (res.code == 200) {
          this.form = res.list;
          this.form.password = "";
          this.form.uid = uid;
        }
      });
    },
  },
};
</script>
<style scoped>
</style>